﻿/*
 * Created by SharpDevelop.
 * User: XPMUser
 * Date: 11/01/2011
 * Time: 12:06
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Windows.Input;

using Microsoft.Win32;

namespace KDG.FoldersSize4
{
	/// <summary>
	/// Description of ScanViewModel.
	/// </summary>
	public class ScanViewModel : ViewModelBase
	{
		private Scanner _scanner = null;
		private FoldersSizeModel _model { get; set; }
		private RelayCommand _scanCommand = null;
		
		public ScanViewModel() {}
		public ScanViewModel(FoldersSizeModel Model) { _model = Model; }
		
		public ICommand ScanCommand
		{
			get {
				if(_scanCommand==null)
					_scanCommand = new RelayCommand( p => 
					                                {
					                                	_scanner = new Scanner();
					                                	_scanner.RunWorkerCompleted+= delegate(object sender, RunWorkerCompletedEventArgs e) 
					                                	{ 
					                                		if(_scanner.Cancelled)
					                                		{
					                                			this.Status = "Canceled";
						                                		this.RootItem = null;
					                                			this.Errors.Clear();
					                                		}
					                                		else
					                                		{
						                                		this.RootItem = (ScannerItem)e.Result;
						                                		this.Status = string.Empty;
					                                		}
						                                	this.IsScanning = false;
					                                		_scanner = null;
					                                	};
					                                	_scanner.ProgressChanged+= delegate(object sender, ProgressChangedEventArgs e) 
					                                	{ 
					                                		if(e.UserState is Exception)
					                                			this.Errors.Add( (Exception)e.UserState);
					                                		if(e.UserState is ScannerReportArg)
					                                		{
					                                			ScannerReportArg sra = (ScannerReportArg)e.UserState;
					                                			this.Status = string.Format("Folders: {0}, files: {1}, [{2}]",
					                                			                            sra.TotalDirectories, sra.TotalFiles, sra.Name);
					                                		}
					                                	};
					                                	this.IsScanning = true;
					                                	this.Errors.Clear();
					                                	this.RootItem = null;
					                                	_scanner.RunWorkerAsync(this.RootPath);
					                                },
					                                c=>
					                                {
					                                	return !(IsScanning || string.IsNullOrEmpty(this.RootPath));
					                                }
					                               );
				
				return _scanCommand;
			}
		}
		private RelayCommand _stopCommand = null;
		public ICommand StopCommand
		{
			get {
				if(_stopCommand==null)
					_stopCommand = new RelayCommand( p => 
					                                { 
					                                	if(_scanner!=null)
					                                		_scanner.CancelAsync();
					                                },
					                                c => 
					                                { 
					                                	return this.IsScanning;
					                                }
					                               );
				
				return _stopCommand;
			}
		}
		private RelayCommand _openCommand = null;
		public ICommand OpenCommand
		{
			get {
				if(_openCommand==null)
					_openCommand = new RelayCommand( p => 
					                                { 
					                                	FolderBrowserDialog fbd = new FolderBrowserDialog();
					                                	
					                                	if(fbd.ShowDialog()== DialogResult.OK)
					                                	{
					                                		this.RootPath = fbd.SelectedPath;
					                                		this.RootItem = null;
					                                	}
					                                });
				
				return _openCommand;
			}
		}
		private RelayCommand _openInNewWindowCommand = null;
		public ICommand OpenInNewWindowCommand
		{
			get {
				if(_openInNewWindowCommand==null)
					_openInNewWindowCommand = new RelayCommand( p => 
					                                { 
					                                           	this.OpenInNewWindowEvent(this, null);
					                                });
				
				return _openInNewWindowCommand;
			}
		}
		private RelayCommand _openInExplorerCommand = null;
		public ICommand OpenInExplorerCommand
		{
			get {
				if(_openInExplorerCommand==null)
					_openInExplorerCommand = new RelayCommand( p => 
					                                { 
					                                          	try
					                                          	{
					                                          		Process.Start(SelectedItem.FullName);
					                                          	}
					                                          	catch(Exception ex)
					                                          	{
					                                          		MessageBox.Show(ex.Message);
					                                          	}
					                                });
				
				return _openInExplorerCommand;
			}
		}
		public bool IsTotalStatusVisible { get { return this.RootItem!=null; } }
		private ScannerItem _selectedItem = null;
		public ScannerItem SelectedItem 
		{ 
			get {return _selectedItem;}
			set {
				if (_selectedItem==value)
					return;
				
				_selectedItem = value;
				OnPropertyChanged("SelectedItem");
			}
		}
		public string RootPath
		{
			get { return _model.RootPath; }
			set {
				if(_model.RootPath==value)
					return ;
				_model.RootPath = value;
				OnPropertyChanged("RootPath");
			}
		}
		public ScannerItem RootItem
		{
			get { return _model.RootItem; }
			set {
				if(_model.RootItem==value)
					return ;
				_model.RootItem = value;
				OnPropertyChanged("RootItem");
				OnPropertyChanged("IsTotalStatusVisible");
			}
		}
		public ObservableCollection<Exception> Errors
		{
			get { return _model.Errors; }
		}
		private string _status = string.Empty;
		public string Status 
		{
			get { return _status; }
			set {
				if(_status==value)
					return ;
				_status = value;
				OnPropertyChanged("Status");
			}
		}
		public bool IsScanning {
			get { return _model.IsScanning; }
			set { 
				if(_model.IsScanning == value)
					return;
				_model.IsScanning = value;
				OnPropertyChanged("IsScanning");
			}
		}
		
		public event EventHandler OpenInNewWindowEvent = delegate {};
	}
}
